package yhao.infra.web.interceptor.logger.support;

import com.alibaba.fastjson.JSONObject;
import nl.basjes.parse.useragent.UserAgent;
import org.apache.commons.lang3.StringUtils;
import yhao.infra.web.common.util.CommonWebUtil;
import yhao.infra.web.common.util.UserAgentUtil;
import yhao.infra.web.interceptor.logger.context.RequestContext;

import jakarta.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.util.Date;

import static yhao.infra.common.logger.CommonLoggerFactory.*;


/**
 * @author yoara
 * <p> Created on 2019/4/19 </p>
 */
public class StatWebLoggerBuilder implements LoggerBuilder{
    /**
     * 访问时间#请求标示#应用名#执行状态#执行时间#请求域名#请求URI#请求参数#响应结果#refere(来源)
     * #客户端IP#服务端IP#操作系统#浏览器信息#
     */
    @Override
    public String doLog(HttpServletRequest request, RequestContext digestContext) throws Exception {
        String errMsg = digestContext.getErrMsg();
        boolean success = StringUtils.isEmpty(errMsg);

        String userAgent = request.getHeader(USER_AGENT_NAME);
        UserAgent clientAgent = UserAgentUtil.analyzer(userAgent);


        long startTime = digestContext.getTimeKey();
        long endTime = System.currentTimeMillis();
        String id = getValue(digestContext.getRequestId());
        String sid = getValue(digestContext.getSessionId());
        String visitTime = getValue(new Date(startTime));
        String appName = getValue(digestContext.getAppName());

        String times = getValue(endTime - startTime);
        String url = getValue(CommonWebUtil.getHostContext());
        String uri = replaceUnNeed(request.getRequestURI());

        String _errMsg    = getValue(errMsg);
        String _success   = getValue(success);
        String referer   = replaceUnNeed(request.getHeader("Referer"));
        String ip        = StringUtils.join(CommonWebUtil.getIpAddr(request),",");
        String host      = getValue(InetAddress.getLocalHost().getHostAddress());
        String system    = getValue(clientAgent.getValue(UserAgent.OPERATING_SYSTEM_NAME_VERSION));
        String browser   = getValue(clientAgent.getValue(UserAgent.AGENT_NAME_VERSION));

        JSONObject result = new JSONObject();

        result.put("id",id);
        result.put("sid",sid);
        result.put("visitTime",visitTime);
        result.put("appName",appName);

        result.put("times",times);
        result.put("url",url);
        result.put("uri",uri);

        if(digestContext.getForm()!=null){
            String form = getValue(JSONObject.toJSONString(digestContext.getForm()));
            String formClass = getValue(digestContext.getForm().getClass().getName());

            result.put("form",form);
            result.put("formClass",formClass);
        }
        result.put("errMsg",_errMsg);
        result.put("success",_success);
        result.put("referer",referer);
        result.put("ip",ip);
        result.put("host",host);
        result.put("system",system);
        result.put("browser",browser);

        return result.toJSONString();
    }
}
